<include file='Index/header' />
<div class="row">
	<br>
	<div class="col-sm-12">
		<div class="col-sm-2"></div>
		<div class="col-sm-1" style="padding-top:5px;width:120px">
			<label class="control-label">发布日期：</label>
		</div>
		<div class="col-sm-2">
			<input id="start_date" class="form-control"></input>
		</div>
		<div class="col-sm-1" style="padding-top:5px;width:10px">至</div>
		<div class="col-sm-2">
			<input id="end_date" class="form-control"></input>
		</div>
	</div>

	<div class="col-sm-12">
		<br>
		<div class="col-sm-2">
			<ul id="repository_tree" class="ztree"></ul>
		</div>
		<div class="col-sm-10">
		<table id="secondhand_product_table" class="table table-hover table-bordered table-striped table-striped">
			<thead>
				<tr>
					<td>A仓库</td>
					<td>B仓库</td>
					<td>内容简介</td>
					<td>发布者</td>
					<td>发布时间</td>
					<td>操作</td>
				</tr>
			</thead>
			<tbody>
			</tbody>
		</table>
		</div>
	</div>
	<br>
</div>

<!-- modal begin -->
<div id="secondhand_product_modal" class="modal fade">
    <div class="modal-dialog" >
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">二手物品</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-sm-12">
                        <div class="col-sm-1"></div>
                        <div class="col-sm-3">
                            <label class="control-label">发布内容：</label>
                        </div>
                        <div class="col-sm-6">
                            <span id="content" class="">sad</span>
                        </div>
                    </div>

                    <div class="col-sm-12">
                    	<br>
                      <div class="col-sm-1"></div>
                      <div class="col-sm-3">
                        <img id="imgurl1" style="width:100%">
                      </div>
                      <div class="col-sm-3">
                        <img id="imgurl2" style="width:100%">
                      </div>
                      <div class="col-sm-3">
                        <img id="imgurl3" style="width:100%">
                      </div>
                    </div>

                    <div class="col-sm-12">
                  		<br>
                      <div class="col-sm-1"></div>
                      <div class="col-sm-3">
                          <label class="control-label">发布者：</label>
                      </div>
                      <div class="col-sm-6">
                          <span id="name" class="control-label"></span>
                      </div>
                    </div>

                    <div class="col-sm-12">
                    	<br>
                      <div class="col-sm-1"></div>
                      <div class="col-sm-3">
                          <label class="control-label">发布时间：</label>
                      </div>
                      <div class="col-sm-6">
                          <span id="createtime" class="control-label"></span>
                      </div>
                    </div>
                </div>
            </div>

            <div class="modal-footer">
                <button id="btn" class="btn btn-primary"></button>
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
<!-- modal end -->

<include file='Index/footer' />
<script>
$(document).ready(function() {
		loadRepositoryTree('repository_tree', true);
		loadDatetimepicker('start_date');
		loadDatetimepicker('end_date');
		loadTable();
});
var table;
function loadTable(){
	table = $('#secondhand_product_table').DataTable({
		"bProcessing": true,
		"bDestroy": true,
		"sPaginationType" : "input",
		"oLanguage" : {
              "sLengthMenu": "每页显示 _MENU_ 条记录",
              "sZeroRecords": "抱歉， 没有找到",
              "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
              "sInfoEmpty": "没有数据",
              "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
              "sZeroRecords": "没有检索到数据",
               "sSearch": "搜索: &nbsp",
              "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
              }
          }, 
		"ajax":'{:U("Admin/Secondhand/getSecondhandProduct/")}',
		"columns": [
					{ "data": 'a_repository_id' },
					{ "data": 'b_repository_id' },
	        { "data": 'content' },
	        { "data": 'user' },
	        { "data": 'createtime' },
	        { "data": 'btn' }
    ],
    "columnDefs": [
    {
    	"targets": [0,1],
    	"visible":false
    },
    {
    	"targets": 3,
    	"render": function (data, type, row, table) {
        return data.name;
      }
    },
    {
    	"targets": -1,
    	"data": null,
    	"render": function (data, type, row, table) {
    		var btn = '审核通过';
    		var status = '1';
    		if(row.status != '0'){
					btn = '审核不通过';
					status = '0';
    		}
        return '<button class="btn btn-info" onclick="view(\''+row.secondhand_product_id+'\')">查看</button>  <button class="btn btn-primary" onclick="modStatus(\''+row.secondhand_product_id+'\', \''+status+'\')">'+btn+'</button>  <button class="btn btn-warning" onclick="del(\''+row.secondhand_product_id+'\')">删除</button>';
      },
    }]
	});

$.fn.dataTable.ext.search.push(
	    function( settings, data, dataIndex ) {
	        var min = new Date($('#start_date').val());
	        var max = new Date($('#end_date').val());
	        var now = new Date( data[4] ); 

	        if ( ( isNaN( min ) && isNaN( max ) ) ||
	             ( isNaN( min ) && now <= max ) ||
	             ( min <= now   && isNaN( max ) ) ||
	             ( min <= now   && now <= max ) )
	        {
	            return true;
	        }
	        return false;
	    }
	);	

	$('#start_date, #end_date').datetimepicker().on('changeDate', function(e){
      table.draw();
  });
	
}

function view(secondhand_product_id){
	//编辑按钮点击，弹出编辑框
	$.ajax({
			url: '{:U("Admin/Secondhand/getSecondhandProduct/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"secondhand_product_id":secondhand_product_id
			}
		})
		.success(function(data){
			$("#content").html(data.content);
			$("#imgurl1").attr('src', data.imgurl1);
			$("#imgurl2").attr('src', data.imgurl2);
			$("#imgurl3").attr('src', data.imgurl3);
			$("#name").html(data.user.name);
			$("#createtime").html(data.createtime);

			var btn = '审核通过';
  		var status = '1';
  		if(data.status != '0'){
				btn = '审核不通过';
				status = '0';
  		}
			$("#btn").attr('onclick', 'modStatus(\''+data.secondhand_product_id+'\', \''+status+'\')');
			$("#btn").html(btn);
			

			$('#secondhand_product_modal').modal({
				backdrop : "static"
			});
	});
}

function modStatus(secondhand_product_id, status){
	$.ajax({
			url: '{:U("Admin/Secondhand/updateSecondhandProduct/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"secondhand_product_id":secondhand_product_id,
				"status":status 
			}
		})
		.success(function(data){
			location.reload();
	});
}

function del(secondhand_product_id){
	if(!confirm("确认删除该二手物品吗？")){
		return;
	}
	$.ajax({
			url: '{:U("Admin/Secondhand/deleteSecondhandProduct/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"secondhand_product_id":secondhand_product_id
			}
		})
		.success(function(data){
			location.reload();
	});
}

function searchARepository(val){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(0).search(val).draw();
}

function searchBRepository(val){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(1).search(val).draw();
}

</script>
